home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / s342q08.lha / libmem.c < prev    next >
C/C++ Source or Header  |  1994-06-15  |  3KB  |  145 lines

  1. /*  This is a set of functions that will replace
  2. *  malloc(), calloc(), realloc(), and free()
  3. *  from the standard C libraries.  These routines
  4. *  link their allocations into the task's memory list
  5. *  and all allocated memory not freed during the
  6. *  execution of the program will be freed upon task removal.
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <exec/types.h>
  11. #include <exec/tasks.h>
  12. #include <exec/memory.h>
  13. #include <proto/exec.h>
  14. #define ALLOCERROR 0x80000000
  15. void mem_debug(char *, long);
  16. struct MemList *FindMemListFromMemPtr(void *MemPtr);
  17. ULONG FindMemSizeFromMemListAndPtr(struct MemList *MList, void *MPtr);
  18. void free(void *oldblock)
  19.   {
  20.   struct MemList *MList;
  21.   MList = FindMemListFromMemPtr(oldblock);
  22.   if (MList)
  23.     {
  24.     Remove((struct Node *)MList);
  25.     FreeEntry(MList);
  26.  
  27.     }
  28.   else
  29.     {
  30.     mem_debug("could not find to free:",(long)oldblock);
  31.  
  32.     };
  33.  
  34.   }
  35. void *malloc(unsigned int size)
  36.   {
  37.   void *AllocBlock;
  38.   struct MemList *NeedMList;
  39.   struct MemList mneed;
  40.   AllocBlock = (void *)0;
  41.   if (size)
  42.     {
  43.     mneed.ml_NumEntries = 1;
  44.     mneed.ml_ME[0].me_Reqs = MEMF_CLEAR;
  45.     mneed.ml_ME[0].me_Length = size;
  46.     NeedMList = (struct MemList *)AllocEntry((struct MemList *)&mneed);
  47.     if (!((ULONG)NeedMList & (ULONG)ALLOCERROR))
  48.       {
  49.       AllocBlock = NeedMList->ml_ME[0].me_Addr;
  50.       AddTail(&(FindTask(0)->tc_MemEntry), (struct Node *)NeedMList);
  51.  
  52.       }
  53.  
  54.     }
  55.   return(AllocBlock);
  56.  
  57.   }
  58. void mem_debug(st,xp)
  59. char *st;
  60. long xp;
  61.   {
  62.   FILE *fp;
  63.   fp = fopen("memory_error.log","a");
  64.   fprintf(fp," %8s: %08.8lx\n",st,xp);
  65.   fclose(fp);
  66.  
  67.   }
  68. void *calloc(unsigned int size, unsigned int count)
  69.   {
  70.   if (!size || !count)  return((void *)0);
  71.   return(malloc(size * count));
  72.  
  73.   }
  74. void *realloc(void *oldblock, unsigned int size)
  75.   {
  76.   void *newblock;
  77.   struct MemList *MLPtr;
  78.   unsigned int bsize, csize;
  79.   newblock = (void *)0;
  80.   if (oldblock && size)
  81.     {
  82.     MLPtr = FindMemListFromMemPtr(oldblock);
  83.     if (MLPtr)
  84.       {
  85.       bsize = (unsigned int)FindMemSizeFromMemListAndPtr(MLPtr, oldblock);
  86.       if (bsize)
  87.         {
  88.         newblock = malloc(size);
  89.         if (newblock)
  90.           {
  91.           csize = (bsize > size) ? size : bsize;
  92.           CopyMem(oldblock, newblock, csize);
  93.           free(oldblock);
  94.  
  95.           }
  96.  
  97.         }
  98.  
  99.       }
  100.  
  101.     }
  102.   return(newblock);
  103.  
  104.   }
  105. struct MemList *FindMemListFromMemPtr(void *MemPtr)
  106.   {
  107.   struct MemList *MList, *MListPtr;
  108.   int loop;
  109.   MList = (struct MemList *)0;
  110.   MListPtr = (struct MemList *)(FindTask(0)->tc_MemEntry.lh_Head);
  111.   while(MListPtr && !MList)
  112.     {
  113.     for (loop = 0; ((loop < MListPtr->ml_NumEntries) && !MList); loop++)
  114.       {
  115.       if (MListPtr->ml_ME[loop].me_Addr == (APTR)MemPtr)
  116.         {
  117.         MList = MListPtr;
  118.  
  119.         }
  120.  
  121.       }
  122.     MListPtr = (struct MemList *)MListPtr->ml_Node.ln_Succ;
  123.  
  124.     }
  125.   return(MList);
  126.  
  127.   }
  128. ULONG FindMemSizeFromMemListAndPtr(struct MemList *MList, void *MPtr)
  129.   {
  130.   ULONG MSize;
  131.   int loop;
  132.   MSize = 0L;
  133.   for (loop = 0; ((loop < MList->ml_NumEntries) && !MSize); loop++)
  134.     {
  135.     if (MList->ml_ME[loop].me_Addr == (APTR)MPtr)
  136.       {
  137.       MSize = MList->ml_ME[loop].me_Length;
  138.  
  139.       }
  140.  
  141.     }
  142.   return(MSize);
  143.  
  144.   }
  145.